//========================================================================
// This file was generated using the MyGeneration tool in combination
// with the Gentle.NET Business Entity template, $Rev: 1104 $
//========================================================================
using System;
using System.Collections;
using Gentle.Common;
using Gentle.Framework;

namespace GrewalCRM.Entities
{
	/// <summary>
	/// Instances of this class represent the properties and methods of a row in the table <b>CRM_Users</b>.
	/// </summary>
	[TableName("CRM_Users", CacheStrategy.Temporary)]
	public class CRMUsers : BaseEntitiesGrewalCRM
	{
		#region Members
		private bool isChanged;
		private int userID;
		private string loginID;
		private string password;
		private int role;
		private string userName;
		private string address;
		private string mobileNo;
		private DateTime doj;
		private bool isActive;
		#endregion
			
		#region Constructors
		/// <summary>
		/// Create a new instance using the default constructor
		/// </summary>
		public CRMUsers()
		{
			isChanged = true;
		}

		/// <summary> 
		/// Create a new object using the minimum required information (all not-null fields except 
		/// auto-generated primary keys). 
		/// </summary> 
		public CRMUsers(string loginID, string password, int role, string userName, string mobileNo, DateTime doj, bool isActive)
		{
			isChanged = true;
			userID = 0;
			this.loginID = loginID;
			this.password = password;
			this.role = role;
			this.userName = userName;
			this.mobileNo = mobileNo;
			this.doj = doj;
			this.isActive = isActive;
		}

		/// <summary> 
		/// Create a new object by specifying all fields (except the auto-generated primary key field). 
		/// </summary> 
		public CRMUsers(string loginID, string password, int role, string userName, string address, string mobileNo, DateTime doj, bool isActive)
		{
			isChanged = true;
			this.loginID = loginID;
			this.password = password;
			this.role = role;
			this.userName = userName;
			this.address = address;
			this.mobileNo = mobileNo;
			this.doj = doj;
			this.isActive = isActive;
		}
			
		/// <summary> 
		/// Create an object from an existing row of data. This will be used by Gentle to 
		/// construct objects from retrieved rows. 
		/// </summary> 
		public CRMUsers(int userID, string loginID, string password, int role, string userName, string address, string mobileNo, DateTime doj, bool isActive)
		{
			this.userID = userID;
			this.loginID = loginID;
			this.password = password;
			this.role = role;
			this.userName = userName;
			this.address = address;
			this.mobileNo = mobileNo;
			this.doj = doj;
			this.isActive = isActive;
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Indicates whether the entity is changed and requires saving or not.
		/// </summary>
		public bool IsChanged
		{
			get { return isChanged; }
		}

		/// <summary>
		/// Property relating to database column UserID
		/// </summary>
		[TableColumn("UserID", NotNull=true), PrimaryKey(AutoGenerated=true)]
		public int UserID
		{
			get { return userID; }
			set { isChanged |= userID != value; userID = value; }
		}

		/// <summary>
		/// Property relating to database column LoginID
		/// </summary>
		[TableColumn("LoginID", NotNull=true)]
		public string LoginID
		{
			get { return loginID.TrimEnd(); }
			set { isChanged |= loginID != value; loginID = value; }
		}

		/// <summary>
		/// Property relating to database column Password
		/// </summary>
		[TableColumn("Password", NotNull=true)]
		public string Password
		{
			get { return password.TrimEnd(); }
			set { isChanged |= password != value; password = value; }
		}

		/// <summary>
		/// Property relating to database column Role
		/// </summary>
		[TableColumn("Role", NotNull = true), ForeignKey("CRM_Roles", "RoleID")]
		public int Role
		{
			get { return role; }
			set { isChanged |= role != value; role = value; }
		}

		/// <summary>
		/// Property relating to database column UserName
		/// </summary>
		[TableColumn("UserName", NotNull=true)]
		public string UserName
		{
			get { return userName.TrimEnd(); }
			set { isChanged |= userName != value; userName = value; }
		}

		/// <summary>
		/// Property relating to database column Address
		/// </summary>
		[TableColumn("Address", NullValue="")]
		public string Address
		{
			get { return address != null ? address.TrimEnd() : string.Empty; }
			set { isChanged |= address != value; address = value; }
		}

		/// <summary>
		/// Property relating to database column MobileNo
		/// </summary>
		[TableColumn("MobileNo", NotNull=true)]
		public string MobileNo
		{
			get { return mobileNo.TrimEnd(); }
			set { isChanged |= mobileNo != value; mobileNo = value; }
		}

		/// <summary>
		/// Property relating to database column DOJ
		/// </summary>
		[TableColumn("DOJ", NotNull=true)]
		public DateTime Doj
		{
			get { return doj; }
			set { isChanged |= doj != value; doj = value; }
		}

		/// <summary>
		/// Property relating to database column IsActive
		/// </summary>
		[TableColumn("IsActive", NotNull=true)]
		public bool IsActive
		{
			get { return isActive; }
			set { isChanged |= isActive != value; isActive = value; }
		}
		#endregion

		#region Storage and Retrieval
	
		/// <summary>
		/// Static method to retrieve all instances that are stored in the database in one call
		/// </summary>
		public static IList ListAll()
		{
			return Broker.RetrieveList(typeof(CRMUsers));
		}

		/// <summary>
		/// Retrieves an entity given it's id.
		/// </summary>
		public static CRMUsers Retrieve(int id)
		{
			Key key = new Key(typeof(CRMUsers), true, "UserID", id);
			return Broker.RetrieveInstance(typeof(CRMUsers), key) as CRMUsers;
		}
		
		/// <summary>
		/// Retrieves an entity given it's id, using Gentle.Framework.Key class.
		/// This allows retrieval based on multi-column keys.
		/// </summary>
		public static CRMUsers Retrieve(Key key)
		{
			return Broker.RetrieveInstance(typeof(CRMUsers), key) as CRMUsers;
		}
		
		/// <summary>
		/// Persists the entity if it was never persisted or was changed.
		/// </summary>
		public override void Persist()
		{
			if (IsChanged || !IsPersisted)
			{
				base.Persist();
				isChanged = false;
			}
		}

		#endregion


		#region Relations

		/// <summary>
		/// Get a list of Calls referring to the current entity.
		/// </summary>
	
		public IList ReferringCallsLastModifiedUser()
		{
			//select * from 'foreigntable'
			SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Calls));

			// where foreigntable.foreignkey = ourprimarykey
			sb.AddConstraint(Operator.Equals, "LastModifiedUser", UserID);

			// passing true indicates that we'd like a list of elements, i.e. that no primary key
			// constraints from the type being retrieved should be added to the statement
			SqlStatement stmt = sb.GetStatement(true);

			// execute the statement/query and create a collection of User instances from the result set
			return ObjectFactory.GetCollection(typeof(Calls), stmt.Execute());

			// TODO In the end, a GentleList should be returned instead of an arraylist
			//return new GentleList( typeof(Calls), this );
		}

		/// <summary>
		/// Get a list of Calls referring to the current entity.
		/// </summary>
	
		public IList ReferringCallsLastModifiedUser()
		{
			//select * from 'foreigntable'
			SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Calls));

			// where foreigntable.foreignkey = ourprimarykey
			sb.AddConstraint(Operator.Equals, "LastModifiedUser", UserID);

			// passing true indicates that we'd like a list of elements, i.e. that no primary key
			// constraints from the type being retrieved should be added to the statement
			SqlStatement stmt = sb.GetStatement(true);

			// execute the statement/query and create a collection of User instances from the result set
			return ObjectFactory.GetCollection(typeof(Calls), stmt.Execute());

			// TODO In the end, a GentleList should be returned instead of an arraylist
			//return new GentleList( typeof(Calls), this );
		}

		/// <summary>
		/// Get a list of Calls referring to the current entity.
		/// </summary>
	
		public IList ReferringCallsLastModifiedUser()
		{
			//select * from 'foreigntable'
			SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Calls));

			// where foreigntable.foreignkey = ourprimarykey
			sb.AddConstraint(Operator.Equals, "LastModifiedUser", UserID);

			// passing true indicates that we'd like a list of elements, i.e. that no primary key
			// constraints from the type being retrieved should be added to the statement
			SqlStatement stmt = sb.GetStatement(true);

			// execute the statement/query and create a collection of User instances from the result set
			return ObjectFactory.GetCollection(typeof(Calls), stmt.Execute());

			// TODO In the end, a GentleList should be returned instead of an arraylist
			//return new GentleList( typeof(Calls), this );
		}

		/// <summary>
		/// Get a list of Calls referring to the current entity.
		/// </summary>
	
		public IList ReferringCallsLastModifiedUser()
		{
			//select * from 'foreigntable'
			SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Calls));

			// where foreigntable.foreignkey = ourprimarykey
			sb.AddConstraint(Operator.Equals, "LastModifiedUser", UserID);

			// passing true indicates that we'd like a list of elements, i.e. that no primary key
			// constraints from the type being retrieved should be added to the statement
			SqlStatement stmt = sb.GetStatement(true);

			// execute the statement/query and create a collection of User instances from the result set
			return ObjectFactory.GetCollection(typeof(Calls), stmt.Execute());

			// TODO In the end, a GentleList should be returned instead of an arraylist
			//return new GentleList( typeof(Calls), this );
		}
		/// <summary>
		///
		/// </summary>
				
		public CRMRoles ReferencedCRMRoles()
		{
			return CRMRoles.Retrieve(Role);
		}
		#endregion
	}
}
